<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">新增留言</div>
    <el-form :inline="true" :rules="rules" ref="ruleForm" :model="form" label-width="100px">
      <el-form-item label="留言" prop="message">
          <el-input placeholder="请留言" v-model="form.message"></el-input>
      </el-form-item>
      <el-form-item label="会员号" prop="username">
        <el-input v-model="admin.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="admin.phone" disabled></el-input>
      </el-form-item>
    </el-form>

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: 'AddMessage',
  data() {
    return {
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
      form:{},
      rules: {
        message: [
          { required: true, message: '请留言', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    save() {
      if(this.admin.role === '0'|| this.admin.role === '1'){
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
              this.form.username = this.admin.username
              this.form.phone = this.admin.phone
            request.post('/message/save', this.form).then(res => {
                console.log(this.form);
              if (res.code === '200') {
                this.$notify.success('新增成功')
                this.$refs['ruleForm'].resetFields()
                this.$router.push("/userMessage")
              } else {
                this.$notify.error(res.msg)
              }
            })
          }
        })
      }else{
        this.$notify.error("您无此权限")
        this.$router.push("/userMessage")
      }
    }
  }
}

</script>
